const { ccclass, property } = cc._decorator;
import WayBaseControl from '../World/WayBaseControl';
import MonsterControl from '../Monster/MonsterControl';

@ccclass
export default class SkillTransfer extends cc.Component {//传送
    @property(cc.Node)
    wayObject: cc.Node = null;
    @property(cc.Node)
    MonsterBody: cc.Node = null;
    @property(cc.Node)
    Destination: cc.Node = null;
    @property
    interval: number = 20;//释放间隔
    reindex: number = 2;
    MonsterControl: MonsterControl = null;
    onLoad() {
        this.MonsterControl = this.MonsterBody.getComponent(MonsterControl);
        this.Destination.opacity = 30;
        this.Destination.active = false;
        this.schedule(this.doSkill, this.interval);
    }
    doSkill() {
        let wayId = HeroHelper.getInstance().uuidWayCollider
        let enterId = this.MonsterControl._uuidWayCollider;
        if (wayId == enterId)
            return;
        if (this.MonsterControl.checkBusy() && this.reindex > 0) {
            this.scheduleOnce(() => {
                this.reindex--;
                this.doSkill();
            }, 1);
            return;
        }
        else {
            let _wayNode = this.wayObject.getChildByUuid(wayId);
            if (_wayNode) {
                let control = _wayNode.getComponent(WayBaseControl);
                let index = control.getEmptyIndex(this.node.width);
                if (index != -1) {
                    this.MonsterControl.setBusy();
                    this.reindex = 3;
                    let Px = control.getItemNodeX(index);
                    this.Destination.y = control.floorY;
                    this.Destination.x = Px;
                    this.Destination.parent = _wayNode;
                    this.Destination.active = true;
                    this.setTween();
                }
                else {
                    //稍后重试
                    this.scheduleOnce(() => {
                        this.reindex--;
                        this.doSkill();
                    }, 1);
                    return;
                }
            }
        }
    }
    setTween() {
        let self = this;
        cc.tween(this.Destination)
            .to(0.8, { opacity: 200 })
            .call(() => {
                self.setMonster();
            })
            .to(0.5, { opacity: 30 })
            .call(() => {
                if (self.Destination) {
                    self.Destination.active = false;
                    self.Destination.parent = self.node;
                }
            })
            .start();
    }
    setMonster() {
        if (this.MonsterControl) {
            this.MonsterControl.setNotBusy();
            this.MonsterControl.birth();
        }
        if (this.Destination)
            this.goWorldPosition(this.Destination, this.MonsterBody);
    }
    //将bNode放到aNode的位置(她们没有共同的父节点时使用)
    goWorldPosition(aNode: cc.Node, bNode: cc.Node, parent?: cc.Node) {
        if (!parent) {
            let p1 = aNode.convertToWorldSpaceAR(cc.v2(0, 0));
            if (bNode?.parent) {
                let p2 = bNode.parent.convertToNodeSpaceAR(p1);
                bNode.x = p2.x;
                bNode.y = p2.y;
            }
        }
        else {
            let p1 = aNode.convertToWorldSpaceAR(cc.v2(0, 0));
            if (parent) {
                let p2 = parent.convertToNodeSpaceAR(p1);
                bNode.x = p2.x;
                bNode.y = p2.y;
            }
        }
    }
}
